/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.CountryDao;
import com.tesam.gedeon.dao.CountryProviderDao;
import com.tesam.gedeon.entity.Country;
import com.tesam.gedeon.entity.CountryProvider;
import com.tesam.gedeon.service.CountryProviderService;
import com.tesam.gedeon.service.CountryService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class CountryProviderServiceImpl implements CountryProviderService {

    @Autowired
    CountryProviderDao countryDao;

    @Override
    public int getCountAll() {
        return countryDao.getCountAll() ;
    }

    @Override
    public int getCountBySearch(CountryProvider countryProvider) {
        return countryDao.getCountBySearch(countryProvider);
    }

    @Override
    public List<CountryProvider> getCountryProviderByLimit(String dataToSort, String directionOfOrder, int startLimit, int endLimit) {
        return countryDao.getCountryProviderByLimit(dataToSort, directionOfOrder, startLimit, endLimit);
    }

    @Override
    public List<CountryProvider> getCountryProviderBySearch(String dataToSort, String directionOfOrder, int startLimit, int endLimit, CountryProvider countryProvider) {
        return countryDao.getCountryProviderBySearch(dataToSort, directionOfOrder, startLimit, endLimit, countryProvider);
    }

    @Override
    public void delete(CountryProvider countryProvider) {
        countryDao.delete(countryProvider);
    }

    @Override
    public void update(CountryProvider countryProvider) {
        countryDao.update(countryProvider);
    }

    @Override
    public int save(CountryProvider countryProvider) {
        return countryDao.save(countryProvider);
    }

    @Override
    public List<CountryProvider> getCountryProviderById(CountryProvider countryProvider) {
        return countryDao.getCountryProviderById(countryProvider);
    }

     @Override
     public String getJsonDataFromArrayList(List<CountryProvider> listCountryProvider, int countRecord){
        JSONArray array = null;
        JSONObject json = null;
        String updateButton , viewButton , deleteButton  = "" ; 
        String countryLink , providerLink = "" ; 
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listCountryProvider.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");
              
                for (CountryProvider countryProvider : listCountryProvider) {
                    JSONArray arrayPut = new JSONArray();
 
                    viewButton = "<a href='view?id="+countryProvider.getId()+"&module=22' ";
                    viewButton+= "class='btn mini blue'><i class='icon-share'></i> View </a>&nbsp;";
                    
                    updateButton = "<a href='update?id="+countryProvider.getId()+"&module=22' class='btn mini green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>&nbsp;";
                    
                    deleteButton = "<a class='btn mini red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+countryProvider.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>";      
                     
                    providerLink = "<a href='../provider/view?module=33&id="+countryProvider.getProvider().getId()+"'>" ;
                    providerLink+= StringEscapeUtils.escapeHtml4(countryProvider.getProvider().getName().trim());
                    providerLink+= "</a>";
                    
                    countryLink = "<a href='../mediationcountry/view?module=20&id="+countryProvider.getCountry().getId()+"'>";
                    countryLink+= countryProvider.getCountry().getName();
                    countryLink+= "</a>";
                    
                    arrayPut.put(countryProvider.getCountryProviderCode().trim()); 
                    arrayPut.put(countryLink);
                    arrayPut.put(countryProvider.getProvider().getCode().trim());
                    arrayPut.put(providerLink);
                    arrayPut.put(viewButton+updateButton+deleteButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", countRecord);
            json.put("iTotalDisplayRecords", countRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
     }
 
     
}
